<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\logic\UserLogic;
use app\common\model\salesman\Register;
use app\common\model\shop\Order;
use app\common\utils\Utils;
use app\common\validate\salesman\RegisterValidate;
use think\Db;

/**
 * Token接口
 */
class H5Api extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function h5qrcode() {

    }


    /**
     * 我的业绩
     */
    public function getPerformance() {
        $start = input('start', '');
        $end = input('end', '');
        $where = [];
        if ($start) {
            $where['createtime'] = ['egt', strtotime($start)];
        }
        if ($end) {
            $where['createtime'] = ['elt', strtotime($end)];
        }

        $userLogic = new UserLogic(UID);
        $list = $userLogic->getChildren()->getPerformance($where);

        $page = input('page/d',     1);
        $sum = 0;
        if ($page == 1) {
            $sum = $userLogic->getAllPerformance($where);
        }
        $this->success('我的业绩', [
            'sum' => $sum,
            'list' => $list
        ]);
    }

    /**
     * 拜访登记
     */
    public function visit() {
        try {

            (new RegisterValidate())->goCheck();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $register = new Register();
        $post = $this->request->post();
        $post['user_id'] = UID;
        $register->allowField(true)->data($post)->save();

        $this->success('登记成功');
    }


    public function detail() {
        $id = input('id/d', 0);
        $detail = Order::get($id);
        $detail['niuname'] = Db::name('shop_goods')->where('id', $detail['goods_id'])->value('name');
        $this->success('详情', $detail);
    }
}
